using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;
using System.IO;

namespace CriteriaPack.BandwidthCriterion
{
    public class BandWidthInfo
    {
        public NetworkInterface networkInterface;

        public long lngBytesSend = 0;
        public long lngBytesReceived = 0;

        public BandWidthInfo()
        {
           
        }

        public int DownloadFilesAndGetBandwidth()
        {

            int results = 0;

            for (int i = 0; i < 4; i++)
            {
                try
                {
                    System.Net.WebClient wc = new System.Net.WebClient();
                    DateTime dt1 = DateTime.Now;
                    byte[] data = wc.DownloadData(EPiServer.Configuration.Settings.Instance.SiteUrl + "/BandWidthVirtualGroup/data_100k.txt?sid=" + new Random().Next());
                    DateTime dt2 = DateTime.Now;

                    results += Convert.ToInt32((data.Length * 0.008) / (dt2 - dt1).TotalSeconds);
                }
                catch (Exception)
                {
                    //Logg error
                }
            }

            if (results == 0)
                return 0;

            return Convert.ToInt32(results / 4);

        }   
    }
}
